package com.artfii;

import com.artfii.fluentsql.core.jdbc.DefaultConnectionProvider;
import com.artfii.fluentsql.core.jdbc.FluentConnection;
import com.artfii.fluentsql.core.jdbc.FluentSqlConfig;
import com.artfii.fluentsql.core.jdbc.FluentSqlQuery;
import com.artfii.fluentsql.core.tools.PrintUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import junit.framework.TestCase;
import org.junit.Assert;

import java.util.List;

/**
 * Func:   : FLUENTSQL 测试基类，包含了基本的创表及数据
 *
 * @author : Lqf(leeton)
 * @date : 2021/11/30.
 */
public class BaseDb extends TestCase {

    private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").create();

    protected FluentSqlQuery fluentSqlQuery;
    protected FluentConnection connection;

    @Override
    protected void setUp() throws Exception {
        FluentSqlConfig config = new FluentSqlConfig().build("dev");
        connection = new FluentConnection(new DefaultConnectionProvider(), config);
        connection.setSql_hack_check(false);
        fluentSqlQuery = new FluentSqlQuery(connection);
        createTable();
    }

    /**
     * 建表
     */
    public void createTable() {
        PrintUtil.whiteLn("DO CREATE TABLE ===================");
        String drop = "DROP TABLE IF EXISTS `user`";
        fluentSqlQuery.execute(drop);
        String sql =
                "CREATE TABLE `user`\n" +
                        "(\n" +
                        "id INT(11) NOT NULL COMMENT '主键ID',\n" +
                        "dept_id INT(11) NOT NULL COMMENT '部门ID',\n" +
                        "user_name VARCHAR(30) NULL DEFAULT NULL COMMENT '名称',\n" +
                        "age INT(3) NULL DEFAULT NULL COMMENT '年龄',\n" +
                        "money DECIMAL (13,3) NULL DEFAULT NULL COMMENT 'money',\n" +
                        "role INT(2) NULL DEFAULT NULL COMMENT '测试',\n" +
                        "phone VARCHAR(13) NULL DEFAULT NULL COMMENT '手机号码',\n" +
                        "create_time DATETIME NULL DEFAULT NULL COMMENT '创建日期',\n" +
                        "update_time DATETIME NULL DEFAULT NULL COMMENT '修改日期',\n" +
                        "PRIMARY KEY (id)\n" +
                        ")";

        Integer row = fluentSqlQuery.execute(sql);
        Assert.assertTrue("CREATE TABLE SUCC", row >= 0);
    }

    protected void print(String lable ,Object obj) {
        String json = gson.toJson(obj);
        PrintUtil.info(lable,json);
    }

    protected void print(String lable,List<Object> objectList) {
        String json = gson.toJson(objectList, List.class);
        PrintUtil.wran(lable,json);
    }


}
